package hn.cch.advanced.concurrent.locks;

public class DeadlockDemo {

    public static void main(String[] args) {
        Object object0 = new Object();
        Object object1 = new Object();

        new Thread(() -> {
            synchronized (object0) {
                System.out.println(Thread.currentThread().getName() + "synchronized object0");

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                synchronized (object1) {
                    System.out.println(Thread.currentThread().getName() + "synchronized object1");
                }
            }
            System.out.println(Thread.currentThread().getName() + ":stop");
        }, "thread0").start();

        new Thread(() -> {
            synchronized (object1) {
                System.out.println(Thread.currentThread().getName() + "synchronized object1");

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                synchronized (object0) {
                    System.out.println(Thread.currentThread().getName() + "synchronized object0");
                }
            }
            System.out.println(Thread.currentThread().getName() + ":stop");
        }, "thread1").start();
    }

}
